package com.itheima.service.impl;

import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PageQuery;
import com.itheima.service.ClazzService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ClazzServiceImpl implements ClazzService {

    private final ClazzMapper clazzMapper;

    @Override
    public PageBean queryClazzByCondition(PageQuery pageQuery) {
        //总数
        Integer total = clazzMapper.queryClazzCount();
        log.info("total数值为{}", total);
        //每页大小
        Integer pageSize = pageQuery.getPageSize();

        //当前页
        Integer page = pageQuery.getPage();

        //开始索引
        Integer start = (page - 1) * pageSize;

        List<Clazz> clazzes = clazzMapper.queryClazzByCondation(start, pageSize, pageQuery);

        PageBean pageBean = new PageBean(total, clazzes);

        return pageBean;
    }

    @Override
    public void addClazz(Clazz clazz) {

        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);

    }

    @Override
    public Clazz queryClazzById(Integer id) {
        Clazz clazz = clazzMapper.queryClazzById(id);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazzMapper.updateClazz(clazz);
    }

    @Override
    public void deleteClazzById(Integer id) {
        clazzMapper.deleteClazzById(id);
    }
}
